<?php
/**
 * Copyright (c) 2009, Microsoft
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Microsoft nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Microsoft ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Microsoft BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @category   Microsoft
 * @package    WebErrorPHPToolkit
 * @subpackage WebConfigXsd
 * @version    $Id: WebConfigXsd.php, v 1.0 2009-10-09 08:00:00Z unknown $
 * @copyright  Copyright (c) 2009, Microsoft (http://www.microsoft.com)
 * @author     a-jetann   
 * 
 * Validates web.config.xml against XML Schema web.config.xmd.
 */


/**
 * Validates web.config.xml document against XML Schema web.config.xsd.
 *
 * @return boolean
 */
function web_error_validate_web_config()
{
  try
  {
    $xdom = new DOMDocument();
    if(!$xdom->load('web.config.xml'))
    {
      throw new WebToolkit_WebConfigXsd_Exception("Error loading as DOM documument 'web.config.xml' file.");
    }
    
    // Enable user error handling
    libxml_use_internal_errors(true);
    libxml_clear_errors();
    
    if (!$xdom->schemaValidate('web.config.xsd'))
    {
      web_error_libxml_trigger_errors();
      return false;
    }
  }
  catch (WebToolkit_WebConfigXsd_Exception $ex)
  {
    throw $ex;
  }
  catch (Exception $ex)
  {
    throw new WebToolkit_WebConfigXsd_Exception( $ex->getMessage() );
    return false;
  }
  
  return true;
}

/**
 * List error that caused XML Schema validation
 * to fail.
 * 
 * @param $error
 * @return void
 */
function web_error_libxml_trigger_error($error)
{
  $error_message = "";
  
  switch ($error->level) 
  {
    case LIBXML_ERR_WARNING:
      $error_message .= "Warning {$error->code}: ";
      break;
    
    case LIBXML_ERR_ERROR:
      $error_message .= "Error {$error->code}: ";
      break;
      
    case LIBXML_ERR_FATAL:
      $message .= "Fatal Error {$error->code}: ";
      break;
  }
  
  $error_message .= trim($error->message);
  
  if ($error->file)
  {
    $error_message .= " in {$error->file}";
  }
  
  if ($error->line > 0)
  {
    $error_message .= " on line {$error->line}.";
  }
  
  throw new WebToolkit_WebConfigXsd_Exception( $error_message );
}


/**
 * Collect all errors that caused XML Schema validation.
 *  
 * @return void
 */
function web_error_libxml_trigger_errors() 
{
  $errors = libxml_get_errors();
  foreach ($errors as $error) {
    web_error_libxml_trigger_error($error);
  }
  
  libxml_clear_errors();
}
?>